//
//  NXSegementPageView.m
//  NXScrollPageView
//
//  Created by N-X on 2018/3/24.
//  Copyright © 2018年 MRNAN. All rights reserved.
//

#import "NXSegementPageView.h"


@interface NXSegementPageView()
@property(nonatomic,copy)NSArray * titles;
@property (nonatomic ,weak)UIViewController * parentViewController;
@property (nonatomic ,weak)NXSegementTitleStyle *  segementStyle;
@property (nonatomic ,strong)NXScrollSegementView * segementTitleView;
@property (nonatomic ,strong)NXSegementContentView * contentView;
@property (nonatomic ,weak)id<NXSegementPageDelegate>delegate;

@end

@implementation NXSegementPageView

- (instancetype)init
{
    return [self initWithFrame:CGRectZero];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    NXSegementTitleStyle * style = [[NXSegementTitleStyle alloc]init];
    return [self initWithFrame:frame titles:nil parentViewController:nil segementStyle:style delegate:nil];
}

- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles parentViewController:(UIViewController *)parentViewController segementStyle:(NXSegementTitleStyle *)segementStyle delegate:(id<NXSegementPageDelegate>)delegate
{
    if (self = [super initWithFrame:frame]) {
        _titles = titles;
        _parentViewController = parentViewController;
        _segementStyle = segementStyle;
        _delegate = delegate;
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    [self addSubview:self.segementTitleView];
    [self addSubview:self.contentView];
}

#pragma mark - lazy load

- (NXScrollSegementView *)segementTitleView
{
    if (!_segementTitleView) {
        _segementTitleView = [[NXScrollSegementView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 40) andTitles:self.titles segementStyle:self.segementStyle];
        __weak typeof(self)weakSelf = self;
        _segementTitleView.titleLabelClickBlock = ^(NSInteger index) {
            [weakSelf.contentView setContentOffSet:CGPointMake(self.contentView.frame.size.width * index, 0) animated:YES];
        };
    }
    return _segementTitleView;
}

- (NXSegementContentView *)contentView
{
    if (!_contentView) {
        _contentView = [[NXSegementContentView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.segementTitleView.frame), self.bounds.size.width, self.bounds.size.height - CGRectGetMaxY(self.segementTitleView.frame)) segementView:self.segementTitleView segementStyle:self.segementStyle parantViewController:self.parentViewController delegate:self.delegate];
    }
    return _contentView;
}

@end
